const router = require('express').Router()
const {db,pool}=require('../db/db')
const uploadSQL=require('../db/uploadSQL')
const fs = require('fs')

router.post('/upimg', (req, res, next) => {
  console.log("我正在发布...")
  let arr = req.files[0];//取到文件（照片）
  fs.rename('public/image/'+arr.filename,'public/image/'+arr.filename+'.jpg',(error)=>{
    if (error) {
      console.log(error);
    } else {
      console.log('ok');
    }
  })
	let obj = new Object();//定义一个对象
	obj.origin ='http://localhost:8888/image/'+ arr.filename+'.jpg';//拼接，把照片地址改成服务器能获取的地址
  pool.query(uploadSQL.insert,[req.body.userId,req.body.title,obj.origin,req.body.price,req.body.campuls,req.body.quality,req.body.category,req.body.email,req.body.phone,new Date(),0,0],(err,result)=>{undefined
    if(err) throw err;
    console.log(result);
  });
  res.send({code:1})
})

module.exports=router
